package com.zhou.util;

import java.math.BigDecimal;
import java.util.regex.Pattern;

/**
 * @author lang.zhou
 * @date 2019/11/8
 */
public class NumberUtil {
    public static boolean upper0(Number o){
        return o != null && o.intValue() > 0;
    }

    public static int forceBetween(int n, int min, int max){
        if(n < min){
            return min;
        }else if(n > max){
            return max;
        }
        return n;
    }

    public static Integer safeToInteger(Object o, Integer dv){
        try{
            if(null == o){
                return dv;
            }else if(o instanceof Integer){
                return (Integer)o;
            }else if(o instanceof Number){
                return ((Number)o).intValue();
            }else{
                return Integer.parseInt(String.valueOf(o));
            }
        }catch(Exception e){
            return dv;
        }
    }
    public static Long safeToLong(Object o, Long dv){
        try{
            if(null == o){
                return dv;
            }else if(o instanceof Long){
                return (Long)o;
            }else if(o instanceof Number){
                return ((Number)o).longValue();
            }else{
                return Long.parseLong(String.valueOf(o));
            }
        }catch(Exception e){
            return dv;
        }
    }
    public static Double safeToDouble(Object o, Double dv){
        try{
            if(null == o){
                return dv;
            }else if(o instanceof Double){
                return (Double)o;
            }else if(o instanceof Number){
                return ((Number)o).doubleValue();
            }else{
                return Double.parseDouble(String.valueOf(o));
            }
        }catch(Exception e){
            return dv;
        }
    }
    public static Float safeToFloat(Object o, Float dv){
        try{
            if(null == o){
                return dv;
            }else if(o instanceof Float){
                return (Float)o;
            }else if(o instanceof Number){
                return ((Number)o).floatValue();
            }else{
                return Float.parseFloat(String.valueOf(o));
            }
        }catch(Exception e){
            return dv;
        }
    }
    /**
     * 判断对象是否为数值
     * @param o     Number或者String
     */
    public static boolean isNumber(Object o){
        if(o instanceof Number){
            return true;
        }else if(o instanceof String){
            return Pattern.matches("^-?\\d+(\\.\\d+)?$",(String)o);
        }
        return false;
    }
    /**
     * 完全匹配数据库NUMBER(len,acc)精度
     * @param numberStr     数值的字符串
     * @param len           最大长度
     * @param acc           精度
     */
    public static boolean isNumeric2(String numberStr, int len, int acc){
        String reg="^-?\\d{1,"+((len-acc))+"}(\\.\\d{1," + acc + "})?$";
        return (numberStr.replace(".","").replace("-","").length()<=len) && Pattern.matches(reg,numberStr);
    }
    /**
     * 匹配数据库NUMBER(len,acc)，精度大于acc的数据库会4舍5入，可忽略
     * @param numberStr     数值的字符串
     * @param len           最大长度
     * @param acc           精度
     */
    public static boolean isNumeric(String numberStr, int len, int acc){
        return Pattern.matches("^-?\\d{1,"+(len-acc)+"}(\\.\\d+)?$",numberStr);
    }
    /**
     * 给数值添加千分符
     * @param obj   Number或者String
     */
    public static String toThousand(Object obj){
        if(!isNumber(obj)){
            return null;
        }
        String strVal=obj.toString();
        boolean mark=strVal.startsWith("-");
        strVal=strVal.replace("-","");
        int doltIndex=strVal.indexOf(".");
        String doltVal=null;
        if(doltIndex > -1){
            doltVal=strVal.substring(doltIndex,strVal.length());
            strVal=strVal.substring(0,doltIndex);
        }
        int n=strVal.length()/3;
        int sec=strVal.length()%3;
        StringBuilder res=new StringBuilder();
        for(int i=n;i>0;i--){
            res.insert(0,strVal.substring(i*3-3+sec,i*3+sec)).insert(0,",");
        }
        if(sec > 0){
            res.insert(0,strVal.substring(0,strVal.length()-n*3));
        }
        if(res.toString().startsWith(",")){
            res.deleteCharAt(0);
        }
        if(mark){
            res.insert(0,"-");
        }
        if(doltVal != null && doltVal.length() > 0){
            res.append(doltVal);
        }
        return res.toString();
    }

    /**
     * Double类型保留n位小数
     */
    public static String formatDouble(Double a,int n){
        return String.format("%."+n+"f", a);
    }
    public static BigDecimal formatDouble(Number a, int n){
        return new BigDecimal(String.format("%."+n+"f", a));
    }
}
